/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cetc.iframe;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Date;
import java.util.List;

import javax.swing.DefaultCellEditor;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import com.cetc.Item;
import com.cetc.Main;
import com.cetc.dao.Dao;
import com.cetc.dao.model.TbSpinfo;

public class KuCunPanDian extends javax.swing.JInternalFrame {

    /**
     * Creates new form KuCunPanDian
     */
    public KuCunPanDian() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jFrame1 = new javax.swing.JFrame();
        jMenu3 = new javax.swing.JMenu();
        jPopupMenu1 = new javax.swing.JPopupMenu();
        jLabel1 = new javax.swing.JLabel();
        czy = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        pdsj = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        pzs = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        jMenu1.setText("jMenu1");

        jMenu2.setText("jMenu2");

        jMenuItem1.setText("jMenuItem1");

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jMenu3.setText("jMenu3");

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("库存盘点");
        setPreferredSize(new java.awt.Dimension(600, 300));

        jLabel1.setText("操作员：");
        czy.setEditable(false);
        czy.setText(Main.czyStateLabel.getText());

        jLabel4.setText("盘点时间：");
        pdsj.setEditable(false);
        pdsj.setText(new Date().toLocaleString());

        jLabel5.setText("品种数：");
        pzs.setEditable(false);
        pzs.setText("0");

        String[] bt = null;
        
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
               
            },
            bt = new String [] {
                "商品名称", "商品编号", "供应商", "产地", "单位", "规格", "单价", "数量", "包装", "盘点数量", "损益数量"
            }
        ));
        jTable1.setRowHeight(24);
        jScrollPane1.setViewportView(jTable1);
        
        DefaultTableModel tableModel = (DefaultTableModel) jTable1.getModel();// 获得表格默认模型
        // 设置盘点字段只接收数字输入
 		final JTextField pdField = new JTextField(0);// “盘点”文本框
 		pdField.setEditable(false);// 设置“盘点”文本框不可编辑
 		pdField.addKeyListener(new PanDianKeyAdapter(pdField));// 为“盘点”文本框添加盘点字段的按键监听器
 		JTextField readOnlyField = new JTextField(0);// “只读”文本框
 		readOnlyField.setEditable(false);// 设置“只读”文本框不可编辑
        DefaultCellEditor pdEditor = new DefaultCellEditor(pdField);// 构造使用“盘点”文本框为参数的盘点编辑器
		DefaultCellEditor readOnlyEditor = new DefaultCellEditor(readOnlyField);// 构造使用“只读”文本框为参数的只读编辑器
		for (int i = 0; i < bt.length; i++) {
			TableColumn column = jTable1.getColumnModel().getColumn(i);// 获得表格中的每一列
			column.setCellEditor(readOnlyEditor);// 设置表格单元为只读格式
		}
		TableColumn pdColumn = jTable1.getColumnModel().getColumn(9);// 盘点数量
		TableColumn syColumn = jTable1.getColumnModel().getColumn(10);// 损益数量
		pdColumn.setCellEditor(pdEditor);// 为盘点数量设置盘点编辑器
		syColumn.setCellEditor(readOnlyEditor);// 为损益数量设置只读编辑器
		// 初始化表格内容
		List kcInfos = Dao.getKucunInfos();// 获得库存信息的集合
		for (int i = 0; i < kcInfos.size(); i++) {// 遍历库存信息的集合
			List info = (List) kcInfos.get(i);// 获得库存信息集合中的元素
			Item item = new Item();// 数据表公共类
			item.setId((String) info.get(0));// 经手人编号
			item.setName((String) info.get(1));// 经手人姓名
			TbSpinfo spinfo = Dao.getSpInfo(item);// 读取商品信息
			Object[] row = new Object[bt.length];// 创建长度为表头数组长度的数组
			if (spinfo.getId() != null && !spinfo.getId().isEmpty()) {// 如果商品编号不为空
				row[0] = spinfo.getSpname();// 添加行数据之“商品名称”
				row[1] = spinfo.getId();// 添加行数据之“商品编号”
				row[2] = spinfo.getGysname();// 添加行数据之“供应商名称”
				row[3] = spinfo.getCd();// 添加行数据之“产地”
				row[4] = spinfo.getDw();// 添加行数据之“商品计量单位”
				row[5] = spinfo.getGg();// 添加行数据之“商品规格”
				row[6] = info.get(2).toString();// 添加行数据之“单价”
				row[7] = info.get(3).toString();// 添加行数据之“数量”
				row[8] = spinfo.getBz();// 添加行数据之“包装”
				row[9] = 0;// 添加行数据之“盘点数量”
				row[10] = 0;// 添加行数据之“损益数量”
				tableModel.addRow(row);// 向表格默认模型中添加行数据
				String pzsStr = pzs.getText();// 获得“品种数”文本框中的文本内容
				int pzsInt = Integer.parseInt(pzsStr);// 将String型的“品种数”转换为int型
				pzsInt++;// “品种数”加1
				pzs.setText(pzsInt + "");// 设置“品种数”文本框中的文本内容
			}
		}

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 568, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(czy, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pdsj)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pzs, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(czy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(pdsj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(pzs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    // 盘点字段的按键监听器
 	private class PanDianKeyAdapter extends KeyAdapter {
 		private final JTextField field;// “盘点”文本框

 		private PanDianKeyAdapter(JTextField field) {// 区分同名变量，并为同名变量赋值
 			this.field = field;
 		}

 		public void keyTyped(KeyEvent e) {// 键入某个键时
 			if (("0123456789" + (char) 8).indexOf(e.getKeyChar() + "") < 0) {// 当前按下的按键没在0123456789的范围里
 				e.consume();// 销毁当前没在key列表里的按键
 			}
 			field.setEditable(true);// 设置“盘点”文本框可编辑
 		}

 		public void keyReleased(KeyEvent e) {// 释放某个键时
 			String pdStr = field.getText();// 获得“盘点”文本框的文本内容
 			String kcStr = "0";// 声明String型的“库存数量”
 			int row = jTable1.getSelectedRow();// 获得被选中的行
 			if (row >= 0) {// 如果表格模型中存在被选中的行
 				kcStr = (String) jTable1.getValueAt(row, 7);// 获得库存数量
 			}
 			try {
 				int pdNum = Integer.parseInt(pdStr);// 将String型的“盘点数量”转换为int型
 				int kcNum = Integer.parseInt(kcStr);// 将String型的“库存数量”转换为int型
 				if (row >= 0) {// 如果表格模型中存在被选中的行
 					jTable1.setValueAt(kcNum - pdNum, row, 10);// 为表格中的“损益数量”赋值
 				}
 				if (e.getKeyChar() != 8)// 当前按下的按键没在0123456789的范围里
 					field.setEditable(false);// “盘点”文本框不可编辑
 			} catch (NumberFormatException e1) {
 				field.setText("0");// 设置“盘点”文本框中的文本内容为0
 			}
 		}
 	}

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(KuCunPanDian.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(KuCunPanDian.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(KuCunPanDian.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(KuCunPanDian.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new KuCunPanDian().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField czy;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField pdsj;
    private javax.swing.JTextField pzs;
    // End of variables declaration//GEN-END:variables
}
